//
//  LoggedOutViewController.m
//  project
//
//  Created by 张振宇 on 15/11/3.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "LoggedOutViewController.h"
#import "PKRequestManager.h"
#import <UIImageView+WebCache.h>
#import "ChangeNameviewController.h"

@interface LoggedOutViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *nickName;
@end

@implementation LoggedOutViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _nickName.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"nickname"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        self.navigationController.navigationBar.backgroundColor = [UIColor magentaColor];
    self.view.backgroundColor = [[UIColor alloc]initWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    UIButton *btn  = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"退出" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.layer.cornerRadius = 5;
    btn.backgroundColor = [[UIColor alloc]initWithRed:221/255.0 green:160/255.0 blue:221/255.0 alpha:1];
    btn.frame = CGRectMake(20, 250, kWidth - 40, 30);
        [btn addTarget:self action:@selector(didLoggout:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    [self addview];
    
}

- (void)addview
{
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 60, kWidth, 60)];
    [self.view addSubview:view1];
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 120, kWidth, 1)];
    [self.view addSubview:view2];
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 121, kWidth, 40)];
    [self.view addSubview:view3];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, kWidth, 60)];
    label1.text = @"更换头像";
    label1.backgroundColor = [UIColor whiteColor];
    [view1 addSubview:label1];
    
    UILabel *label2 =[[UILabel alloc]initWithFrame:CGRectMake(0, 10, kWidth, 40)];
    label2.text = @"昵称";
    label2.backgroundColor = [UIColor whiteColor];
    [view3 addSubview:label2];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didGetPicture)];
    [view1 addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didChangeName)];
    [view3 addGestureRecognizer:tap2];
    
    _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth- 80, 20, 40, 40)];
    _imgView.layer.masksToBounds = YES;
    _imgView.layer.cornerRadius = 20;
    
    if(![[NSFileManager defaultManager] fileExistsAtPath:[[NSUserDefaults standardUserDefaults]valueForKey:@"file"]]) {
        if ([[NSUserDefaults standardUserDefaults]valueForKey:@"avatar_url"]!=nil&&!([[[NSUserDefaults standardUserDefaults]valueForKey:@"avatar_url"]isEqualToString:@""])) {
            [_imgView sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]valueForKey:@"avatar_url"]]];
        }
        else{
            [_imgView setImage:[UIImage imageNamed:@"user-1"]];
        }
    }
    else
    {
        _imgView.image = [UIImage imageNamed:((NSString *)[[NSUserDefaults standardUserDefaults]valueForKey:@"file"])];
    }
    

    [view1 addSubview:_imgView];
    
    _nickName = [[UILabel alloc]initWithFrame:CGRectMake(kWidth - 150, 20, 120, 20)];
    _nickName.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"nickname"];
    [view3 addSubview:_nickName];
}

- (void)didGetPicture
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"更换头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"选择已有照片", nil];
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex == 0) {
        [self openCamera];
    }
    else if (buttonIndex == 1)
    {
        [self openPics];
    }
}

//打开相机
//UIImagePickerController 这个类可以为大家提供照相的功能,以及图片,视频浏览的功能。
- (void)openCamera {
    
    //Available 可用的
    
    if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)])
    {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        //指定用户用相机拍照，获得照片或者视频
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        
        [self presentViewController:imagePicker animated:YES completion:nil];
        
    }
    else
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该设备没有摄像头" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
}


// 打开相册
- (void)openPics {
    
    //创建对象
    UIImagePickerController
    *imagePicker = [[UIImagePickerController alloc] init];
    
    //指定用户从相册中获得图片
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    imagePicker.delegate = self;//遵守UIImagePickerControllerDelegate协议
    
    imagePicker.allowsEditing = YES;// 编辑模式当属性为YES的时候选取完之后能对图片进行放大缩小以及位置移动
    //.因为UIImagePickerController继承于UINavigationController所以有UINavigationItem。
    
    [self presentViewController:imagePicker animated:YES
                     completion:nil];
    
}


//选中照片
//媒体已经选择好了，要做什么事
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    
    //NSLog(@"%@",info);
    
 //   UIImageView *imageView = (UIImageView *)[self.view viewWithTag:101];
    
    //UIImagePickerControllerOriginalImage 原始图片
    
    //UIImagePickerControllerEditedImage 编辑后图片
    UIImage *image = [info objectForKey: UIImagePickerControllerEditedImage];
    
    _imgView.image = image;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"icon.png"]];   // 保存文件的名称
    BOOL result = [UIImagePNGRepresentation(image)writeToFile: filePath    atomically:YES];
    
    [[NSUserDefaults standardUserDefaults]setValue:filePath forKey:@"file"];
    
    [picker
     dismissViewControllerAnimated:YES completion:NULL];
    
}

//取消相册

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    [picker
     dismissViewControllerAnimated:YES completion:NULL];
}


- (void)didChangeName
{
    ChangeNameviewController *changeNameVC = [[ChangeNameviewController alloc]init];
    [self.navigationController pushViewController:changeNameVC animated:YES];
}

-(void)back:(UIBarButtonItem *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didLoggout:(UIButton *)btn
{
    NSUserDefaults *share = [NSUserDefaults standardUserDefaults];
    [share setValue:@"" forKey:@"nickname"];
    [share setValue:@"" forKey:@"nameid"];
    [share setValue:@"" forKey:@"LoginId"];
    [share setValue:@"" forKey:@"avatar_url"];
    [share setValue:@"" forKey:@"file"];
    [PKRequestManager requestWithTypeP:POST urlString:@"http://api.liwushuo.com/v2/account/signout" parDic:nil finish:^(NSData *data) {
        
    } error:^(NSError *error) {
        
    }];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
